home *** CD-ROM | disk | FTP | other *** search
/ Languguage OS 2 / Languguage OS II Version 10-94 (Knowledge Media)(1994).ISO / gnu / bash_114.zip / bash-1.14.2 / builtins / trap.def < prev    next >
Encoding:
Text File  |  1994-03-03  |  4.9 KB  |  205 lines

  1. This file is trap.def, from which is created trap.c.
  2. It implements the builtin "trap" in Bash.
  3.  
  4. Copyright (C) 1987, 1989, 1991 Free Software Foundation, Inc.
  5.  
  6. This file is part of GNU Bash, the Bourne Again SHell.
  7.  
  8. Bash is free software; you can redistribute it and/or modify it under
  9. the terms of the GNU General Public License as published by the Free
  10. Software Foundation; either version 1, or (at your option) any later
  11. version.
  12.  
  13. Bash is distributed in the hope that it will be useful, but WITHOUT ANY
  14. WARRANTY; without even the implied warranty of MERCHANTABILITY or
  15. FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  16. for more details.
  17.  
  18. You should have received a copy of the GNU General Public License along
  19. with Bash; see the file COPYING.  If not, write to the Free Software
  20. Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  21.  
  22. $PRODUCES trap.c
  23.  
  24. $BUILTIN trap
  25. $FUNCTION trap_builtin
  26. $SHORT_DOC trap [arg] [signal_spec]
  27. The command ARG is to be read and executed when the shell receives
  28. signal(s) SIGNAL_SPEC.  If ARG is absent all specified signals are
  29. reset to their original values.  If ARG is the null string this
  30. signal is ignored by the shell and by the commands it invokes.  If
  31. SIGNAL_SPEC is EXIT (0) the command ARG is executed on exit from
  32. the shell.  The trap command with no arguments prints the list of
  33. commands associated with each signal number.  SIGNAL_SPEC is either
  34. a signal name in <signal.h>, or a signal number.  The syntax `trap -l'
  35. prints a list of signal names and their corresponding numbers.
  36. Note that a signal can be sent to the shell with "kill -signal $$".
  37. $END
  38.  
  39. #include <sys/types.h>
  40. #include <signal.h>
  41. #include "../shell.h"
  42. #include "../trap.h"
  43. #include "common.h"
  44.  
  45. /* The trap command:
  46.  
  47.    trap <arg> <signal ...>
  48.    trap <signal ...>
  49.    trap -l
  50.    trap [--]
  51.  
  52.    Set things up so that ARG is executed when SIGNAL(s) N is recieved.
  53.    If ARG is the empty string, then ignore the SIGNAL(s).  If there is
  54.    no ARG, then set the trap for SIGNAL(s) to its original value.  Just
  55.    plain "trap" means to print out the list of commands associated with
  56.    each signal number.  Single arg of "-l" means list the signal names. */
  57.  
  58. /* Possible operations to perform on the list of signals.*/
  59. #define SET 0            /* Set this signal to first_arg. */
  60. #define REVERT 1        /* Revert to this signals original value. */
  61. #define IGNORE 2        /* Ignore this signal. */
  62.  
  63. extern int interactive;
  64.  
  65. trap_builtin (list)
  66.      WORD_LIST *list;
  67. {
  68.   register int i;
  69.   int list_signal_names = 0;
  70.  
  71.   while (list)
  72.     {
  73.       if (ISOPTION (list->word->word, 'l'))
  74.     {
  75.       list_signal_names++;
  76.       list = list->next;
  77.     }
  78.       else if (ISOPTION (list->word->word, '-'))
  79.     {
  80.       list = list->next;
  81.       break;
  82.     }
  83.       else if ((*list->word->word == '-') && list->word->word[1])
  84.     {
  85.       bad_option (list->word->word);
  86.       builtin_error ("usage: trap [-l] [arg] [sigspec]");
  87.       return (EX_USAGE);
  88.     }
  89.       else
  90.     break;
  91.     }
  92.  
  93.   if (list_signal_names)
  94.     {
  95.       int column = 0;
  96.  
  97.       for (i = 0; i < NSIG; i++)
  98.     {
  99.       printf ("%2d) %s", i, signal_name (i));
  100.       if (++column < 4)
  101.         printf ("\t");
  102.       else
  103.         {
  104.           printf ("\n");
  105.           column = 0;
  106.         }
  107.     }
  108.       if (column != 0)
  109.     printf ("\n");
  110.       return (EXECUTION_SUCCESS);
  111.     }
  112.  
  113.   if (list)
  114.     {
  115.       char *first_arg = list->word->word;
  116.       int operation = SET, any_failed = 0;
  117.  
  118.       if (signal_object_p (first_arg))
  119.     operation = REVERT;
  120.       else
  121.     {
  122.       list = list->next;
  123.       if (*first_arg == '\0')
  124.         operation = IGNORE;
  125.       else if (first_arg[0] == '-' && !first_arg[1])
  126.         operation = REVERT;
  127.     }
  128.  
  129.       while (list)
  130.     {
  131.       int sig;
  132.  
  133.       sig = decode_signal (list->word->word);
  134.  
  135.       if (sig == NO_SIG)
  136.         {
  137.           builtin_error ("%s: not a signal specification",
  138.                  list->word->word);
  139.           any_failed++;
  140.         }
  141.       else
  142.         {
  143.           switch (operation)
  144.         {
  145.           case SET:
  146.             set_signal (sig, first_arg);
  147.             break;
  148.  
  149.           case REVERT:
  150.             restore_default_signal (sig);
  151.  
  152.             /* Signals that the shell treats specially need special
  153.                handling. */
  154.             switch (sig)
  155.               {
  156.               case SIGINT:
  157.             if (interactive)
  158.               signal (SIGINT, sigint_sighandler);
  159.             else
  160.               signal (SIGINT, termination_unwind_protect);
  161.             break;
  162.  
  163.               case SIGQUIT:
  164.             /* Always ignore SIGQUIT. */
  165.             signal (SIGQUIT, SIG_IGN);
  166.             break;
  167.               case SIGTERM:
  168. #if defined (JOB_CONTROL)
  169.               case SIGTTIN:
  170.               case SIGTTOU:
  171.               case SIGTSTP:
  172. #endif /* JOB_CONTROL */
  173.             if (interactive)
  174.               signal (sig, SIG_IGN);
  175.             break;
  176.               }
  177.             break;
  178.  
  179.           case IGNORE:
  180.             ignore_signal (sig);
  181.             break;
  182.         }
  183.         }
  184.       list = list->next;
  185.     }
  186.       return ((!any_failed) ? EXECUTION_SUCCESS : EXECUTION_FAILURE);
  187.     }
  188.  
  189.   for (i = 0; i < NSIG; i++)
  190.     {
  191.       char *t, *p;
  192.  
  193.       p = trap_list[i];
  194.  
  195.       if (p == (char *)DEFAULT_SIG)
  196.     continue;
  197.  
  198.       t = (p == (char *)IGNORE_SIG) ? (char *)NULL : single_quote (p);
  199.       printf ("trap -- %s %s\n", t ? t : "''", signal_name (i));
  200.       if (t)
  201.     free (t);
  202.     }
  203.   return (EXECUTION_SUCCESS);
  204. }
  205.